//
//  HZJImageView.swift
//  HZJTextView
//
//  Created by HZJ on 17/1/11.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

class HZJImageView: UIView {
    
    
    ///当前绘制图形的线条颜色
    fileprivate var currentColor:UIColor = UIColor.black
    ///用之以判断是否是新开始一条线的绘制
    fileprivate var beginOnePath:Bool = false
    ///当前线条的宽度
    fileprivate var lineWidth:CGFloat = 2
        ///下部按钮数量
    fileprivate var btnCount:Int = 5
    
    
    ///截取图片
    func getImage() -> UIImage?{
        UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)
        guard let ctx = UIGraphicsGetCurrentContext() else { return nil }
        self.layer.render(in: ctx)
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else {  return nil }
        UIGraphicsEndImageContext()
        return self.getTheImage(image, size: CGSize(width: 100, height: 100))
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
        setUpUI()
    }

    fileprivate func setUpUI() {
        addSubview(bottomV)
        btnCount = 5
        bottomV.backgroundColor = UIColor.white
        
        for btn in btnArray {
            btn.removeFromSuperview()
        }
        btnArray.removeAll()
        let btnW = CGFloat(Int(bottomV.frame.width / CGFloat(btnCount)))
        for index in 0 ..< btnCount{
            let btn = UIButton()
            if index == self.btnCount - 1 {
                btn.backgroundColor = self.backgroundColor
                btn.setTitle("WIPE", for: UIControl.State())
                btn.setTitleColor(UIColor.black, for: UIControl.State())
            }else{
                btn.backgroundColor = UIColor.randomColor()
            }
            btn.addTarget(self, action: #selector(choiceLineColor(_:)), for: UIControl.Event.touchUpInside)
            btn.frame = CGRect(x: btnW * CGFloat(index), y: 0, width: btnW, height: bottomV.frame.height)
            btnArray.append(btn)
            bottomV.addSubview(btn)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        bottomV.frame = CGRect(x: 0, y: frame.height - 50, width: frame.width, height: 50)
        let btnW = CGFloat(Int(bottomV.frame.width / CGFloat(btnCount)))
        for index in 0 ..< btnArray.count {
            btnArray[index].frame = CGRect(x: btnW * CGFloat(index), y: 0, width: btnW, height: bottomV.frame.height)
        }
    }

    ///选择图片颜色
    @objc func choiceLineColor(_ btn:UIButton){
        if let color = btn.backgroundColor {
            self.lineWidth = color == self.backgroundColor ? 10 : 2
            self.currentColor = color
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.beginOnePath = true
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let t = touches.first
        let point = t!.location(in: t?.view)
        
        if self.beginOnePath {
            let path = HZJBezierPath()
            path.currentColor = self.currentColor
            path.lineWidth = self.lineWidth
            path.move(to: point)
            self.pathArray.append(path)
            self.beginOnePath = false
        }
        self.pathArray.last?.addLine(to: point)
        self.setNeedsDisplay()
    }
    
    ///绘制
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        for path in pathArray {
            path.currentColor.set()
            path.lineCapStyle = .round
            path.lineJoinStyle = .round
            path.stroke()
        }
    }
    
    ///根据图片及尺寸获取图片
    fileprivate func getTheImage(_ image:UIImage,size:CGSize) ->UIImage{
        UIGraphicsBeginImageContext(size)
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return image!
    }
    ///路径集合
    fileprivate lazy var pathArray:[HZJBezierPath] = []
    ///下部工具栏
    fileprivate lazy var bottomV:UIView = UIView()
    ///下部工具栏上按钮集合
    fileprivate var btnArray:[UIButton] = []

}

class HZJBezierPath:UIBezierPath {
    var currentColor:UIColor = UIColor.black
}
